<?php

class itoolsService extends iceService {

	//过滤数组的空白字符
	private function _trim($data = array())
	{
		if (! empty($data))
		{
			foreach ($data as $k => $v)
			{
				if (is_array($v))
				{
					$v = $this->_trim($v);
				}
				else
				{
					$v = trim($v);
				}
				
				$data[$k] = $v;
			}
		}
		
		return $data;
	}
	
	//获得所有的字段类型
	public function get_field_types($bygroup = TRUE)
	{
		$sql = "select * from sys_field_type";
		$data = $this->dao->db->get_all_sql($sql);
		
		$res = array();
		
		if ($bygroup == FALSE)
		{
			if (! empty($data))
			{
				foreach ($data as $v)
				{
					$res[$v['type_name']] = $v;
				}
			}
		}
		else
		{
			if (! empty($data))
			{
				foreach ($data as $v)
				{
					$res[$v['type_group']][$v['type_name']] = $v;
				}
			}
		}
				
		return $res;
	}
	
	//获得所有的业务字段
	public function get_field_base()
	{
		$sql = "select a.* from sys_field_base a, sys_field_type b where a.field_type=b.type_name order by a.field_sort asc";
		$data = $this->dao->db->get_all_sql($sql);
		
		$res = array();
		if (! empty($data))
		{
			foreach ($data as $v)
			{
				$res[$v['field_id']] = $v;
			}
		}
		
		return $res;
	}
	//插入业务字段
	public function insert_field_base($data = array())
	{
		if (empty($data))	return FALSE;
		
		$data = $this->_deal_field_data($data);
		$data = $this->_trim($data);
		
		return $this->dao->db->insert($data, 'sys_field_base');
	}
	//更新业务字段
	public function update_field_base($data = array(), $id = 0)
	{
		if (empty($data))	return FALSE;
		
		$data = $this->_deal_field_data($data);
		$data = $this->_trim($data);
		
		$this->dao->db->update($id, $data, 'sys_field_base', 'field_id');
		
		return TRUE;
	}
	//删除业务字段
	public function delete_field_base($id = 0)
	{
		return $this->dao->db->delete($id, 'sys_field_base', 'field_id');
	}
	//预处理业务字段的内容
	private function _deal_field_data($data = array())
	{
		$types = $this->get_field_types(FALSE);
		
		if (empty($data['field_length']))
		{
			$data['field_length'] = $types[$data['field_type']]['type_length'];
		}
		if (empty($data['field_default']))
		{
			$data['field_default'] = $types[$data['field_type']]['type_default'];
		}
		
		return $data;
	}

	//获得所有操作
	public function get_operation_base()
	{
		$sql = "select * from sys_operation_base order by op_sort asc";
		$data = $this->dao->db->get_all_sql($sql);
		
		$res = array();
		if (! empty($data))
		{
			foreach ($data as $v)
			{
				$res[$v['op_id']] = $v;
			}
		}
		
		return $res;
	}
	//插入操作
	public function insert_operation_base($data = array())
	{
		if (empty($data))	return FALSE;
		
		$data = $this->_trim($data);
		
		return $this->dao->db->insert($data, 'sys_operation_base');
	}
	//更新操作
	public function update_operation_base($data = array(), $id = 0)
	{
		if (empty($data))	return FALSE;
		
		$data = $this->_trim($data);

		$this->dao->db->update($id, $data, 'sys_operation_base', 'op_id');
		
		return TRUE;
	}
	//删除操作
	public function delete_operation_base($id = 0)
	{
		return $this->dao->db->delete($id, 'sys_operation_base', 'op_id');
	}
	
	//获得所有业务表
	public function get_table_base()
	{
		$sql = "select * from sys_table_base order by field_sort asc";
		$data = $this->dao->db->get_all_sql($sql);
		
		$res = array();
		if (! empty($data))
		{
			foreach ($data as $v)
			{
				$res[$v['table_name']][$v['tb_id']] = $v;
			}
		}
		
		return $res;
	}
	//插入业务表
	public function insert_table_base($data = array())
	{
		if (empty($data))	return FALSE;
		
		$data = $this->_trim($data);
		
		return $this->dao->db->insert($data, 'sys_table_base');
	}
	//更新业务表
	public function update_table_base($data = array(), $id = 0)
	{
		if (empty($data))	return FALSE;
		
		$data = $this->_trim($data);

		$this->dao->db->update($id, $data, 'sys_table_base', 'tb_id');
		
		return TRUE;
	}
	//删除业务表
	public function delete_table_base($id = 0)
	{
		return $this->dao->db->delete($id, 'sys_table_base', 'tb_id');
	}
	
}
